<!--
 * @Author: byron
 * @Date: 2022-01-09 13:13:31
 * @LastEditTime: 2022-01-27 15:32:24
-->
<template>
    <transition name="dialog-fade">
        <div class="dialog-mask" v-if="visible" @click.self="handleClose()">
            <!-- <transition name="size"> -->
            <div
                class="dialog"
                :style="'margin-top:' + top + ';width:' + width"
                v-show="visible"
            >
                <i class="iconfont icon-delete" @click="handleClose"></i>
                <div class="title" v-if="title">
                    <slot name="title"></slot>
                </div>
                <div class="body">
                    <slot name="body"></slot>
                </div>

                <div class="footer" v-if="footer">
                    <slot name="footer"></slot>
                </div>
            </div>
            <!-- </transition> -->
        </div>
    </transition>
</template>

<script>
export default {
    name: 'Dialog',
    props: {
        title: {
            type: Boolean,
            default: false,
        },
        footer: {
            type: Boolean,
            default: false,
        },
        // 弹框宽度
        width: {
            type: String,
            default: '700px',
        },
        // 弹框距离顶部距离
        top: {
            type: String,
            default: '15vh',
        },
        visible: {
            type: Boolean,
            default: false,
        },
    },
    methods: {
        handleClose() {
            //visible是父组件传过来的props，子组件不能直接修改，需要子传父
            this.$emit('handleClose', false)
        },
    },

    data() {
        return {
            isDestroy: false,
        }
    },
    mounted() {
        const body = document.querySelector('body')
        if (body.append) {
            body.append(this.$el)
        } else {
            body.appendChild(this.$el)
        }
    },
}
</script>
<style lang="less" scoped>
.dialog-mask {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    overflow: hidden;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 11;
    &.dialog-fade-enter-active {
        animation: dialog-mask 0.6s;
        .dialog {
            animation: dialog 0.6s;
        }
    }
    &.dialog-fade-leave-active {
        animation: dialog-mask-leave 1s;
        .dialog {
            animation: dialog-leave 1s;
        }
    }

    .dialog {
        position: relative;
        min-width: 384px;
        max-width: 90%;
        height: 65%;
        padding: 24px 32px 32px;
        border-radius: 21px;
        margin: auto;
        background-color: #fff;
        i {
            position: absolute;
            top: 12px;
            right: -37px;
            font-size: 24px;
            color: rgb(255, 255, 255);
            cursor: pointer;
            z-index: 12;
        }
        .title {
            padding: 16px 0;
            text-align: center;
            font-size: 20px;
            font-weight: 900;
            color: #555555;
        }

        .body {
            height: 100%;
        }
        // /* 设置滚动条的样式 */
        // &::-webkit-scrollbar {
        //     width: 0px;
        // }
    }
}

@keyframes dialog-mask {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}
@keyframes dialog {
    0% {
        transform: translateY(100px);
        opacity: 0.5;
    }
    100% {
        opacity: 1;
    }
}
@keyframes dialog-mask-leave {
    0% {
        opacity: 1;
    }
    100% {
        opacity: 0;
    }
}
@keyframes dialog-leave {
    0% {
        opacity: 1;
    }
    100% {
        transform: translateY(100px);
        opacity: 0.5;
    }
}
</style>
